/*
 *   Clovis
 *   Copyright (C) 2008 SFWOL
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "CFloat.h"

namespace clovis {

	namespace core {


        CFloat::CFloat(std::string name , float value) : ClovisVariable(name){
            this->value = value;
        }


		CFloat::CFloat(float value) : ClovisVariable(){

			this->value = value;

		}

		CFloat::CFloat() : ClovisVariable(){

			this->value = 0;

		}

		std::string CFloat::getClass(){

			return "CFloat";

		}

		void CFloat::operator = (float value){

			this->value = value;

		}

		void CFloat::operator = (CFloat value){

			this->value = value.get();

		}

		CFloat CFloat::CFloat::operator + (CFloat value){

			return CFloat(this->value + value.get());

		}

		CFloat CFloat::CFloat::operator - (CFloat value){

			return CFloat(this->value - value.get());

		}

		CFloat CFloat::CFloat::operator * (CFloat value){

			return CFloat(this->value * value.get());

		}

		CFloat CFloat::CFloat::operator / (CFloat value){

			return CFloat(this->value / value.get());

		}

		float CFloat::get(){

			return this->value;

		}

		float CFloat::toFloat(){

			return this->value;

		}

		CInteger CFloat::toCInteger(CONVERSION_METHOD method){

			CInteger new_value;

			CString tmp = this->toCString();
			std::vector<CString> exploded = tmp.explode(".");

			switch(method){

				case NEAREST:

					if(exploded[1].toCInteger() >= 5)
						new_value = exploded[0].toCInteger() + 1;
					else
						new_value = exploded[0].toCInteger();

					break;

				case TRUNCATE:
					new_value = exploded[0].toCInteger();
					break;

			}

			return new_value;

		}

		std::string CFloat::toString(){

			std::stringstream ss;
			std::string str;

			ss << this->value;
			ss >> str;

			return str;

		}

		CString CFloat::toCString(){

			CString tmp = this->toString();
			return tmp;

		}

	};

};
